<template>
    <div class="home">
        <headerView class="header" :headerTitle="headerTitle" />
        <div class="home-content">
            <div id="MyCharts" style="width: 100%;height: 200px;"></div>
            <div id="MyChartsLine" style="width: 100%;height: 200px;"></div>
            <div ref="scrollContainer" id="content-list">
                <div v-for="item in tabItem" :key="item.id" class="list-item">
                    <div style="display: flex;justify-content: space-between;font-weight: bold;">
                        <div>{{ item.sampleDataName }}</div>
                        <div>{{ item.contactInformation }}</div>
                    </div>
                    <div class="ellipsis">{{ item.specificInformation }}</div>
                    <div>创建日期：{{ item.createDate }}</div>
                </div>
            </div>
        </div>
    </div>
</template>

<script setup name="Home">
import { onMounted, ref } from 'vue';
import service from '@/utils/request';
import headerView from '@/components/Header/index.vue';
import * as echarts from 'echarts';
const scrollContainer = ref(null);
const initChartXAxis = ref([]);
const initChartSeries = ref([]);
const initChartLineXAxis = ref([]);
const initChartLineSeries = ref([]);
const tabItem = ref([]);
const headerTitle = ref({
    title: '全国 GDP 总览',
    aline: 'left'
});
const paging = ref({
    totalItems: 0,
    currentPage: 0,
    pageSize: 10,
})
const inteTable = () => {
    service({
        url: "api/SampleData?" + "pageIndex=" + paging.value.currentPage + "&pageSize=" + paging.value.pageSize,
        method: 'get',
    }).then(res => {
        if (res != undefined) {
            paging.value.totalItems = res.data.totalCount;
            res.data.pageData.forEach(item => {
                item.createDate = item.createDate.split('T')[0];
                tabItem.value.push(item);
            });
        }
    });
};
const handleScroll = () => {
    const container = scrollContainer.value;
    if (container.scrollTop + container.clientHeight >= container.scrollHeight - 25) {
        if (tabItem.value.length < paging.value.totalItems) {
            paging.value.currentPage++;
            inteTable();
        }
    }
};

const initChart = async () => {
    await getxAxis("GDPData");
    var chartDom = document.getElementById('MyCharts');
    var myChart = echarts.init(chartDom);
    var option;
    option = {
        title: { text: '全国GDP数据柱状图' },
        tooltip: {
            show: false,
            trigger: 'axis',
            axisPointer: {
                type: 'shadow'
            }
        },
        grid: {
            left: '3%',
            right: '4%',
            bottom: '3%',
            containLabel: true
        },
        xAxis: [{
            type: 'category',
            data: initChartXAxis.value,
            axisTick: {
                alignWithLabel: true
            }
        }],
        yAxis: [{
            type: 'value'
        }],
        series: [{
            name: 'Direct',
            type: 'bar',
            barWidth: '60%',
            data: initChartSeries.value,
        }]
    };
    option && myChart.setOption(option);
}
const initChartLine = async () => {
    await getxAxis("GDPGrowthRateData");
    var chartDom = document.getElementById('MyChartsLine');
    var myChart = echarts.init(chartDom);
    var option;
    option = {
        title: { text: '全国GDP数据柱状图' },
        grid: {
            left: '3%',
            right: '4%',
            bottom: '3%',
            containLabel: true
        },
        xAxis: {
            type: 'category',
            data: initChartLineXAxis.value,
        },
        yAxis: {
            type: 'value'
        },
        series: [{
            data: initChartLineSeries.value,
            type: 'line'
        }]
    };
    option && myChart.setOption(option);
};
const queryData = async (url) => {
    var data;
    await service({
        url: "api/" + url,
        method: 'get',
    }).then(res => {
        if (res != undefined) {
            data = res.data;
        }
    });
    return data;
};
const getxAxis = async (url) => {
    var data = await queryData(url);
    for (let i = 0; i < data.length; i++) {
        if (url == "GDPData") {
            initChartXAxis.value.push(data[i].gdpYear);
            initChartSeries.value.push(data[i].data);
        } else {
            initChartLineXAxis.value.push(data[i].gdpYear);
            initChartLineSeries.value.push(data[i].data);
        }
    }
};
onMounted(() => {
    inteTable();
    initChart();
    initChartLine();
    scrollContainer.value.addEventListener('scroll', handleScroll);
});
</script>

<style scoped>
.ellipsis {
    display: -webkit-box;
    -webkit-line-clamp: 1;
    -webkit-box-orient: vertical;
    overflow: hidden;
    text-overflow: ellipsis;
}

.home {
    widows: 100%;
    height: 100%;
    margin: 0;
    padding: 0;
    background-color: white;
}

.header {
    position: fixed;
    top: 0;
    left: 0;
    z-index: 1000;
}

.home-content {
    margin: 0 auto;
    width: 90%;
    height: calc(100% - 60px);
    padding-top: 60px;
    opacity: 1;
}

#content-list {
    height: calc(100% - 410px);
    overflow: scroll;
}

.list-item {
    margin: 5px 0;
    box-shadow: 1px 1px 10px #cccccc30;
    background-color: aquamarine;
    padding: 15px;
    border-radius: 8px;
    height: 67px;
}
</style>